package com.xx.secret.demo;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson.JSONObject;
import com.xx.secret.enums.OpenEnum;
import com.xx.secret.core.IOpenVerify;
import com.xx.secret.util.Base64Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：wangcheng
 * @date 2024/3/13
 * 三方对接加解密处理
 **/
@Slf4j
@Service
public class XXVerifyDemoServiceI implements IOpenVerify {

    private static final String aesKey = "PemfsAof+FL/WS9zaxz17A==";
    private static  AES aes = SecureUtil.aes(Base64Util.decode(aesKey));

    @Override
    public OpenEnum getApiLoan() {
        return OpenEnum.XX;
    }

    @Override
    public void verify(JSONObject param) {
        log.info("完成请求参数的验签");
    }

    @Override
    public String decryptParam(JSONObject param) {
        log.info("完成参数的解密");
        String data = param.getString("data");
        return aes.decryptStr(data);
    }

    @Override
    public Object encryptResponse(Object resp) {
        log.info("响应加密");
        return resp;
    }

    @Override
    public Object exceptionResponse(Exception e) {
        Map<String,Object> errorMap = new HashMap<>();
        errorMap.put("code","-1");
        errorMap.put("message",e.getMessage());
        return errorMap;
    }

    @Override
    public JSONObject getConfig() {
        // 改为从配置中读取
        return new JSONObject();
    }

}
